﻿<!DOCTYPE html>
<html lang="zh-cn" class="fullscreen-bg" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <!--全局通用框架样式 begin-->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!--/*@thymesVar id="title" type=""*/-->
    <title>汽车信息</title>
    <link href="/coral/images/favicon.ico" rel="icon">
    <link href="/assets/libs/layui/css/layui.css" rel="stylesheet"/>
    <link href="/assets/module/admin.css" rel="stylesheet">

    <link href="//cdn.bootcss.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script type="text/javascript" src="/assets/libs/layui/layui.js"></script>
    <script type="text/javascript" src="/assets/js/common.js"></script>
    <script type="text/javascript" src="/coral/js/custom.js"></script>
    <script type="text/javascript" src="../js/common.js"></script>
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">车牌号:</label>
                                        <div class="layui-input-inline">
                                            <input name="carNumber" class="layui-input" placeholder="请输入车牌号"/>
                                        </div>
                                    </div>
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">车架号:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="frame" class="layui-input" placeholder="请输入车架号"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">发动机号:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="engine" class="layui-input" placeholder="请输入发动机号"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">车品牌:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="brand" class="layui-input" placeholder="请输入车品牌"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">车型号:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="model" class="layui-input" placeholder="请输入车型号"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">单位id:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="companyId" class="layui-input" placeholder="请输入单位id"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">科室id:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="departmentId" class="layui-input" placeholder="请输入科室id"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                <!--                    <div class="layui-inline">-->
                <!--                        <label class="layui-form-label">状态:</label>-->
                <!--                        <div class="layui-input-inline">-->
                <!--                            <input name="state" class="layui-input" placeholder="请输入状态"/>-->
                <!--                        </div>-->
                <!--                    </div>-->
                                    <div class="layui-inline">&emsp;
                                        <button class="layui-btn icon-btn" lay-filter="searchBtn" lay-submit>
                                            <i class="layui-icon">&#xe615;</i>搜索
                                        </button>&nbsp;
                                        <button shiro:hasPermission="car:export" id="exportBtn" class="layui-btn icon-btn"
                                                type="button">
                                            <i class="layui-icon">&#xe67d;</i>导出
                                        </button>
                                    </div>
                                </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>


<script type="text/html" id="formDialog1">
    <form id="dataForm1" lay-filter="dataForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">单位:</label>
            <div class="layui-input-block">
                <!--                <input name="companyId" placeholder="请输入单位id" class="layui-input"-->
                <!--                       lay-verType="tips" lay-verify="required" required/>-->
                <select name="companyId2" lay-filter="companyId2">

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label ">科室:</label>
            <div class="layui-input-block">
                <!--                <input name="departmentId" placeholder="请输入科室id" class="layui-input"-->
                <!--                       lay-verType="tips" lay-verify="required" required/>-->
                <select name="departmentId2" lay-filter="departmentId2">

                </select>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveBtn" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="chakan">查看</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="fpxx">分配信息</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="formDialog">
    <form id="dataForm" lay-filter="dataForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">公司:</label>
            <div class="layui-input-block">
                <input name="company" placeholder="请输入公司名称" class="layui-input"
                       lay-verType="tips" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">图片:</label>
            <div class="layui-input-block">
                <img class="logoshow" width="298px" height="128px"/>
                <button type="button" readonly class="layui-btn" id="test">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">车牌号:</label>
            <div class="layui-input-block">
                <input name="carNumber" id="carNumber" placeholder="请输入车牌号" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车架号:</label>
            <div class="layui-input-block">
                <input name="frame" placeholder="请输入车架号" class="layui-input"
                       lay-verType="tips" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label ">发动机号:</label>
            <div class="layui-input-block">
                <input name="engine" placeholder="请输入发动机号" class="layui-input"
                       lay-verType="tips"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">车品牌:</label>
            <div class="layui-input-block">
                <input name="brand" placeholder="请输入车品牌" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">车型号:</label>
            <div class="layui-input-block">
                <input name="model" placeholder="请输入车型号" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">单位:</label>
            <div class="layui-input-block">
                <!--                <input name="companyId" placeholder="请输入单位id" class="layui-input"-->
                <!--                       lay-verType="tips" lay-verify="required" required/>-->
                <select name="companyId" lay-filter="companyId">

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label ">科室:</label>
            <div class="layui-input-block">
                <!--                <input name="departmentId" placeholder="请输入科室id" class="layui-input"-->
                <!--                       lay-verType="tips" lay-verify="required" required/>-->
                <select name="departmentId" lay-filter="departmentId">

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">描述:</label>
            <div class="layui-input-block">
                <input name="content" placeholder="请输入车辆描述" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">价格:</label>
            <div class="layui-input-block">
                <input name="money" placeholder="请输入租车价格" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>


        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveBtn" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<script type="text/html" id="formDialog4">
    <form id="dataForm4" lay-filter="dataForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label layui-form-required">图片:</label>-->
        <div class="layui-input-block">
            <img class="logoshow" width="598px" height="328px"/>
            <!--                <button type="button" readonly class="layui-btn" id="test">-->
            <!--                    <i class="layui-icon">&#xe67c;</i>上传图片-->
            <!--                </button>-->
        </div>
        <!--        </div>-->
        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label layui-form-required">地址:</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input name="url" placeholder="请输入地址" class="layui-input"-->
        <!--                       lay-verType="tips" lay-verify="required" required/>-->
        <!--            </div>-->
        <!--        </div>-->
        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label layui-form-required">权重:</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input name="weight" placeholder="请输入权重" class="layui-input"-->
        <!--                       lay-verType="tips" lay-verify="required" required/>-->
        <!--            </div>-->
        <!--        </div>-->
        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label layui-form-required">:</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input name="state" placeholder="请输入" class="layui-input"-->
        <!--                       lay-verType="tips" lay-verify="required" required/>-->
        <!--            </div>-->
        <!--        </div>-->

        <!--        <div class="layui-form-item text-right">-->
        <!--            <button class="layui-btn" lay-filter="saveBtn" lay-submit>保存</button>-->
        <!--            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>-->
        <!--        </div>-->
    </form>
</script>
<script>
    layui.use(['layer', 'form', 'upload', 'table', 'util', 'laydate', 'tableX', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var tableX = layui.tableX;
        var admin = layui.admin;
        var upload = layui.upload;
        /* 渲染表格 */
        var cols = [
            {type: 'checkbox'},
            {field: 'company', title: '公司', align: 'center'},
            {field: 'money', title: '价格', align: 'center'},
            {field: 'content', title: '描述', align: 'center'},
            {
                field: 'img', title: '图片', align: 'center', templet: function (d) {
                    return "<img src='" + d.img + "'/>";
                }
            },
            {field: 'carNumber', title: '车牌号', align: 'center'},
            {field: 'frame', title: '车架号', align: 'center'},
            {field: 'engine', title: '发动机号', align: 'center'},
            {field: 'brand', title: '车品牌', align: 'center'},
            {field: 'model', title: '车型号', align: 'center'},
            {
                field: 'companyId', title: '单位', align: 'center', templet: function (d) {
                    var name = "";
                    $.ajax({
                        url: "/company/selCompanyId?id=" + d.companyId,
                        type: 'post',
                        dataType: 'JSON',
                        contentType: 'application/json',
                        async: false,
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("X-Token", C.find("X-Token"));
                        },
                        success: function (res) {
                            if (res.body) {
                                name = res.body.name;
                                ;
                            }
                        }
                    })
                    return name;
                }
            },
            {
                field: 'departmentId', title: '科室', align: 'center', templet: function (d) {
                    var name = "";
                    $.ajax({
                        url: "/department/selDepartmentId?id=" + d.departmentId,
                        type: 'post',
                        dataType: 'JSON',
                        contentType: 'application/json',
                        async: false,
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("X-Token", C.find("X-Token"));
                        },
                        success: function (res) {
                            if (res.body) {
                                name = res.body.name;
                                ;
                            }
                        }
                    })
                    return name;
                }
            },
            // {field: 'state', title: '状态', align: 'center'},
            {title: '操作', toolbar: '#tableBar', align: 'center', minWidth: 240}
        ];

        var insTb = table.render({
            elem: '#dataTable',
            url: '/car/selCarPageAfter',
            page: true,
            toolbar: [shiroToolbar(true, false)].join(''),
            cellMinWidth: 100,
            cols: [cols],
            method: 'post',
            dataType: 'json',
            contentType: 'application/json',
            headers: {"X-Token": C.find("X-Token")},
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.errorCode, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.body.total, //解析数据长度
                    "data": res.body.records //解析数据列表
                };
            },
            request: {
                pageName: 'pageIndex' //页码的参数名称，默认：page
                ,
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            done: function () {
                // 绑定鼠标右键
                tableX.bindCtxMenu('dataTable', shiroBindCtxMenu(false, true));
            }
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj.data);
            } else if (obj.event === 'reset') { // 重置密码
                resetPsw(obj);
            } else if (obj.event === 'fpxx') {
                showEditModel2(obj.data);
            } else if (obj.event === 'chakan') {
                showEditModel3(obj.data);
            }
        });

        function showEditModel3(mData) {
            admin.open({
                type: 1,
                area: '800px',
                title: ('查看'),
                content: $('#formDialog4').html(),
                shadeClose: true,
                success: function (layero, dIndex) {
                    form.val('dataForm4', mData);  // 回显数据

                    if (mData) {
                        $(".logoshow").attr("src",  mData.img);
                        $(".logoshow").show();
                    }

                    //执行实例
                    var img;
                    var uploadInst = upload.render({
                        elem: '#test' //绑定元素
                        , url: '/api/file/upload' //上传接口
                        , done: function (res) {
                            img = res.body.filePathURL;
                            //上传完毕回调
                            $(".logoshow").attr("src",  img);
                            $(".logoshow").show();
                            $("#test").text("更换封面");
                        }
                        , error: function () {
                            //请求异常回调
                        }
                    });

                    form.on('submit(saveBtn)', function (data) {

                        var img = $(".logoshow").attr('src');
                        // $.post(mData ? 'banner/update' : 'banner/save', data.field, function (res) {
                        //     if (res.code == 0) {
                        //         layer.close(dIndex);
                        //         layer.msg(res.msg, {icon: 1});
                        //         insTb.reload({page: {curr: 1}});
                        //     } else {
                        //         layer.msg(res.msg, {icon: 2});
                        //     }
                        // }, 'json');


                        if (mData) {
                            var data = {
                                "img": img,
                                "id": mData.id,
                            }
                            $.ajax({
                                url: '/car/update',
                                type: 'post',
                                data: JSON.stringify(data),
                                dataType: 'JSON',
                                contentType: 'application/json',
                                beforeSend: function (xhr) {
                                    xhr.setRequestHeader("X-Token", C.find("X-Token"));
                                },
                                success: function (res) {
                                    if (res.message == 'success') {
                                        layer.msg('修改成功', {icon: 1})
                                        layer.close(dIndex);
                                        // show();
                                        insTb.reload({page: {curr: 1}});
                                        ;
                                    } else {
                                        layer.msg(res.message);
                                    }
                                    //     if (res.code == 0) {
                                    //     layer.close(dIndex);
                                    //     layer.msg(res.msg, {icon: 1});
                                    //     insTb.reload({page: {curr: 1}});
                                    // } else {
                                    //     layer.msg(res.msg, {icon: 2});
                                    // }
                                }
                            })
                        }
                        return false;
                    });
                }
            });

        }

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        function showEditModel2(mData) {
            admin.open({
                type: 1,
                area: '800px',
                title: ('分配信息'),
                content: $('#formDialog1').html(),
                shadeClose: true,
                success: function (layero, dIndex) {
                    form.val('dataForm1', mData);  // 回显数据

                    $.ajax({
                        url: "/company/selCompanyList",
                        type: 'post',
                        dataType: 'JSON',
                        contentType: 'application/json',
                        async: false,
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("X-Token", C.find("X-Token"));
                        },
                        success: function (res) {

                            var data = res.body;
                            if (data) {
                                var html = "<option value=''>请选择单位</option>";
                                for (var i = 0; i < data.length; i++) {
                                    var tp = data[i];
                                    if (mData) {
                                        if (mData.companyId == tp.id) {
                                            html += "<option value='" + tp.id + "'  selected='selected'>" + tp.name + "</option>"
                                        } else {
                                            html += "<option value='" + tp.id + "'>" + tp.name + "</option>"
                                        }
                                    } else {
                                        html += "<option value='" + tp.id + "'>" + tp.name + "</option>"
                                    }
                                }
                                $("[name='companyId2']").html("");
                                $("[name='companyId2']").append(html);
                                // $("#shoolId");
                                form.render('select');
                            }
                        }
                    })

                    $.ajax({
                        url: "/department/selDepartmentList",
                        type: 'post',
                        dataType: 'JSON',
                        contentType: 'application/json',
                        async: false,
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("X-Token", C.find("X-Token"));
                        },
                        success: function (res) {

                            var data = res.body;
                            if (data) {
                                var html = "<option value=''>请选择科室</option>";
                                for (var i = 0; i < data.length; i++) {
                                    var tp = data[i];
                                    if (mData) {
                                        if (mData.departmentId == tp.id) {
                                            html += "<option value='" + tp.id + "' selected='selected'>" + tp.name + "</option>"
                                        } else {
                                            html += "<option value='" + tp.id + "'>" + tp.name + "</option>"
                                        }
                                    } else {
                                        html += "<option value='" + tp.id + "'>" + tp.name + "</option>"
                                    }
                                }
                                $("[name='departmentId2']").html("")
                                $("[name='departmentId2']").append(html);
                                // $("#shoolId");
                                form.render('select');
                            }
                        }
                    })

                    form.on('select(companyId2)', function (data) {
                        // alert(data.value);
                        $.ajax({
                            url: "/department/selDepartment?companyId=" + data.value,
                            type: 'post',
                            dataType: 'JSON',
                            contentType: 'application/json',
                            async: false,
                            beforeSend: function (xhr) {
                                xhr.setRequestHeader("X-Token", C.find("X-Token"));
                            },
                            success: function (res2) {
                                if (res2.body) {
                                    var list = res2.body;
                                    var html = "<option value=''>请选择科室</option>";
                                    for (var i = 0; i < list.length; i++) {
                                        var sc = list[i];
                                        if (sc) {
                                            html += "<option value='" + sc.id + "'>" + sc.name + "</option>";
                                        }
                                    }

                                    $("[name='departmentId2']").html("")
                                    $("[name='departmentId2']").append(html);
                                    form.render('select');

                                }
                            }
                        })
                    })

                    form.on('submit(saveBtn)', function (data) {
                        var companyId = $("[name='companyId2']").val();
                        var departmentId = $("[name='departmentId2']").val();
                        var data = {
                            "id": mData.id,
                            "companyId": companyId,
                            "departmentId": departmentId
                        }
                        $.ajax({
                            url: '/car/update',
                            type: 'post',
                            data: JSON.stringify(data),
                            dataType: 'JSON',
                            contentType: 'application/json',
                            beforeSend: function (xhr) {
                                xhr.setRequestHeader("X-Token", C.find("X-Token"));
                            },
                            success: function (res) {
                                if (res.message == 'success') {
                                    layer.msg('分配成功', {icon: 1})
                                    layer.close(dIndex);
                                    // show();
                                    insTb.reload({page: {curr: 1}});
                                    ;
                                } else {
                                    layer.msg(res.message);
                                }
                                //     if (res.code == 0) {
                                //     layer.close(dIndex);
                                //     layer.msg(res.msg, {icon: 1});
                                //     insTb.reload({page: {curr: 1}});
                                // } else {
                                //     layer.msg(res.msg, {icon: 2});
                                // }
                            }
                        })
                        return false;
                    });
                }
            });

            /* 渲染laydate */
            laydate.render({
                elem: '#creDate',
            });
            /* 渲染laydate */
            laydate.render({
                elem: '#updDate',
            });
        }


        // 显示编辑弹窗
        function showEditModel(mData) {
            admin.open({
                type: 1,
                area: '800px',
                title: (mData ? '修改' : '添加'),
                content: $('#formDialog').html(),
                shadeClose: true,
                success: function (layero, dIndex) {
                    form.val('dataForm', mData);  // 回显数据
                    if (mData) {
                        $(".logoshow").attr("src", mData.img);
                        $(".logoshow").show();
                    }

                    var img;
                    var uploadInst = upload.render({
                        elem: '#test' //绑定元素
                        , url: '/api/file/upload' //上传接口
                        , headers: {"X-Token": C.find("X-Token")}
                        , done: function (res) {
                            img = res.body.filePathURL;
                            //上传完毕回调
                            $(".logoshow").attr("src", baseURL + img);
                            $(".logoshow").show();
                            $("#test").text("更换封面");
                        }
                        , error: function () {
                            //请求异常回调
                        }
                    });
                    $.ajax({
                        url: "/company/selCompanyList",
                        type: 'post',
                        dataType: 'JSON',
                        contentType: 'application/json',
                        async: false,
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("X-Token", C.find("X-Token"));
                        },
                        success: function (res) {

                            var data = res.body;
                            if (data) {
                                var html = "<option value=''>请选择单位</option>";
                                for (var i = 0; i < data.length; i++) {
                                    var tp = data[i];
                                    if (mData) {
                                        if (mData.companyId == tp.id) {
                                            html += "<option value='" + tp.id + "'  selected='selected'>" + tp.name + "</option>"
                                        } else {
                                            html += "<option value='" + tp.id + "'>" + tp.name + "</option>"
                                        }
                                    } else {
                                        html += "<option value='" + tp.id + "'>" + tp.name + "</option>"
                                    }
                                }
                                $("[name='companyId']").html("");
                                $("[name='companyId']").append(html);
                                // $("#shoolId");
                                form.render('select');
                            }
                        }
                    })

                    $.ajax({
                        url: "/department/selDepartmentList",
                        type: 'post',
                        dataType: 'JSON',
                        contentType: 'application/json',
                        async: false,
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("X-Token", C.find("X-Token"));
                        },
                        success: function (res) {

                            var data = res.body;
                            if (data) {
                                var html = "<option value=''>请选择科室</option>";
                                for (var i = 0; i < data.length; i++) {
                                    var tp = data[i];
                                    if (mData) {
                                        if (mData.departmentId == tp.id) {
                                            html += "<option value='" + tp.id + "' selected='selected'>" + tp.name + "</option>"
                                        } else {
                                            html += "<option value='" + tp.id + "'>" + tp.name + "</option>"
                                        }
                                    } else {
                                        html += "<option value='" + tp.id + "'>" + tp.name + "</option>"
                                    }
                                }
                                $("[name='departmentId']").html("")
                                $("[name='departmentId']").append(html);
                                // $("#shoolId");
                                form.render('select');
                            }
                        }
                    })

                    form.on('select(companyId)', function (data) {

                        // alert(data.value);
                        $.ajax({
                            url: "/department/selDepartment?companyId=" + data.value,
                            type: 'post',
                            dataType: 'JSON',
                            contentType: 'application/json',
                            async: false,
                            beforeSend: function (xhr) {
                                xhr.setRequestHeader("X-Token", C.find("X-Token"));
                            },
                            success: function (res2) {
                                if (res2.body) {
                                    var list = res2.body;
                                    var html = "<option value=''>请选择科室</option>";
                                    for (var i = 0; i < list.length; i++) {
                                        var sc = list[i];
                                        if (sc) {
                                            html += "<option value='" + sc.id + "'>" + sc.name + "</option>";
                                        }
                                    }

                                    $("[name='departmentId']").html("")
                                    $("[name='departmentId']").append(html);
                                    form.render('select');

                                }
                            }
                        })
                    })


                    form.on('submit(saveBtn)', function (data) {
                        var img = $(".logoshow").attr('src');
                        var carNumber = $("#carNumber").val();
                        var frame = $("[name='frame']").val();
                        var engine = $("[name='engine']").val();
                        var brand = $("[name='brand']").val();
                        var model = $("[name='model']").val();
                        var companyId = $("[name='companyId']").val();
                        var departmentId = $("[name='departmentId']").val();
                        var content = $("[name='content']").val();
                        var money = $("[name='money']").val();
                        var company = $("[name='company']").val();
                        if (mData) {
                            var data = {
                                "company":company,
                                "money":money,
                                "content":content,
                                "carNumber": carNumber,
                                "id": mData.id,
                                "frame": frame,
                                "engine": engine,
                                "brand": brand,
                                "model": model,
                                "companyId": companyId,
                                "departmentId": departmentId,
                                "img": img
                            }
                            $.ajax({
                                url: '/car/update',
                                type: 'post',
                                data: JSON.stringify(data),
                                dataType: 'JSON',
                                contentType: 'application/json',
                                beforeSend: function (xhr) {
                                    xhr.setRequestHeader("X-Token", C.find("X-Token"));
                                },
                                success: function (res) {
                                    if (res.message == 'success') {
                                        layer.msg('修改成功', {icon: 1})
                                        layer.close(dIndex);
                                        // show();
                                        insTb.reload({page: {curr: 1}});
                                        ;
                                    } else {
                                        layer.msg(res.message);
                                    }
                                    //     if (res.code == 0) {
                                    //     layer.close(dIndex);
                                    //     layer.msg(res.msg, {icon: 1});
                                    //     insTb.reload({page: {curr: 1}});
                                    // } else {
                                    //     layer.msg(res.msg, {icon: 2});
                                    // }
                                }
                            })
                        } else {
                            var data = {
                                "company":company,
                                "money":money,
                                "content":content,
                                "carNumber": carNumber,
                                "frame": frame,
                                "engine": engine,
                                "brand": brand,
                                "model": model,
                                "companyId": companyId,
                                "departmentId": departmentId,
                                "img": img
                            }

                            $.ajax({
                                url: '/car/insert',
                                type: 'post',
                                data: JSON.stringify(data),
                                dataType: 'JSON',
                                contentType: 'application/json',
                                beforeSend: function (xhr) {
                                    xhr.setRequestHeader("X-Token", C.find("X-Token"));
                                },
                                success: function (res) {
                                    if (res.message == 'success') {
                                        layer.msg('添加成功', {icon: 1})
                                        layer.close(dIndex);
                                        // show();
                                        insTb.reload({page: {curr: 1}});
                                        ;
                                    } else {
                                        layer.msg(res.message);
                                    }
                                    //     if (res.code == 0) {
                                    //     layer.close(dIndex);
                                    //     layer.msg(res.msg, {icon: 1});
                                    //     insTb.reload({page: {curr: 1}});
                                    // } else {
                                    //     layer.msg(res.msg, {icon: 2});
                                    // }
                                }
                            })
                        }
                        return false;
                    });
                }
            });

            /* 渲染laydate */
            laydate.render({
                elem: '#creDate',
            });
            /* 渲染laydate */
            laydate.render({
                elem: '#updDate',
            });
        }

        /* 表格搜索 */
        form.on('submit(searchBtn)', function (data) {
            if (data.field.dateRange) {
                var searchDate = data.field.dateRange.split(' - ');
                data.field.startDate = searchDate[0];
                data.field.endDate = searchDate[1];
            } else {
                data.field.startDate = '';
                data.field.endDate = '';
            }
            data.field.dateRange = undefined;
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 导出excel */
        $('#exportBtn').click(function () {
            var checkRows = table.checkStatus('dataTable');
            if (checkRows.data.length === 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                tableX.exportDataX({
                    cols: insTb.config.cols,
                    data: checkRows.data,
                    fileName: '导出数据'
                });
            }
        });


        /* 删除 */
        window.doDel = function (obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);

                $.ajax({
                    url: '/car/delete?id=' + obj.id,
                    type: 'post',
                    // data: {"id": obj.id},
                    dataType: 'JSON',
                    contentType: 'application/json',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("X-Token", C.find("X-Token"));
                    },
                    success: function (res) {
                        if (res.message == 'success') {
                            layer.msg('删除成功', {icon: 1})
                            // layer.close(dIndex);
                            // show();
                            insTb.reload({page: {curr: 1}});
                            layer.close(loadIndex);

                        } else {
                            layer.msg(res.message);
                        }

                    }
                })
            });
        }

        /* 渲染时间选择 */
        laydate.render({
            elem: 'input[name="dateRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
    });
</script>
</body>
</html>